﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace ly.DAL
{
	/// <summary>
	/// 数据访问类:WorkInfo
	/// </summary>
	public partial class WorkInfo
	{
		public WorkInfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("WorkID", "WorkInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int WorkID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from WorkInfo");
			strSql.Append(" where WorkID=@WorkID ");
			SqlParameter[] parameters = {
					new SqlParameter("@WorkID", SqlDbType.Int,4)			};
			parameters[0].Value = WorkID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(ly.Model.WorkInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into WorkInfo(");
            strSql.Append("WorkName,WorkPicture,UploadTime,WorkIntroduction,WorkVerify,UserName,Recommend,RecommendTime,ActivityID)");
			strSql.Append(" values (");
            strSql.Append("@WorkName,@WorkPicture,@UploadTime,@WorkIntroduction,@WorkVerify,@UserName,@Recommend,@RecommendTime,@ActivityID)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					//new SqlParameter("@WorkID", SqlDbType.Int,4),
					new SqlParameter("@WorkName", SqlDbType.NChar,50),
					new SqlParameter("@WorkPicture", SqlDbType.NChar,250),
					new SqlParameter("@UploadTime", SqlDbType.DateTime),
					new SqlParameter("@WorkIntroduction", SqlDbType.NVarChar,-1),
					new SqlParameter("@WorkVerify", SqlDbType.NChar,10),
					new SqlParameter("@UserName", SqlDbType.NChar,50),
					new SqlParameter("@Recommend", SqlDbType.NChar,2),
					new SqlParameter("@RecommendTime", SqlDbType.DateTime),
                    new SqlParameter("@ActivityID", SqlDbType.Int,4),};
			//parameters[0].Value = model.WorkID;
			parameters[0].Value = model.WorkName;
			parameters[1].Value = model.WorkPicture;
			parameters[2].Value = model.UploadTime;
			parameters[3].Value = model.WorkIntroduction;
			parameters[4].Value = model.WorkVerify;
			parameters[5].Value = model.UserName;
			parameters[6].Value = model.Recommend;
			parameters[7].Value = model.RecommendTime;
            parameters[8].Value = model.ActivityID;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}




        /// <summary>
        /// 添加一条评论，传值为1个disCuss型的 model
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public int AdddisCuss(ly.Model.disCuss model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into disWork(");
            strSql.Append("WorkID,UserName,disContent)");
            strSql.Append(" values (");
            strSql.Append("@WorkID,@UserName,@disContent)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					//new SqlParameter("@WorkID", SqlDbType.Int,4),
                    //new SqlParameter("@WorkName", SqlDbType.NChar,50),
                    //new SqlParameter("@WorkPicture", SqlDbType.NChar,250),
                    //new SqlParameter("@UploadTime", SqlDbType.DateTime),
                    new SqlParameter("@WorkID", SqlDbType.Int,4),
                    new SqlParameter("@UserName", SqlDbType.NChar,50),
					new SqlParameter("@disContent", SqlDbType.NVarChar,-1),
                    //new SqlParameter("@WorkVerify", SqlDbType.NChar,10),
                    //new SqlParameter("@RecommendTime", SqlDbType.DateTime),
                    };
            //parameters[0].Value = model.WorkID;
            parameters[0].Value = model.Workid;
            parameters[1].Value = model.Username;
            parameters[2].Value = model.Content;
            //parameters[3].Value = model.WorkIntroduction;
            //parameters[4].Value = model.WorkVerify;
            //parameters[5].Value = model.UserName;
            //parameters[6].Value = model.Recommend;
            //parameters[7].Value = model.RecommendTime;
            //parameters[8].Value = model.ActivityID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }












		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ly.Model.WorkInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update WorkInfo set ");
			strSql.Append("WorkName=@WorkName,");
			strSql.Append("WorkPicture=@WorkPicture,");
			strSql.Append("UploadTime=@UploadTime,");
			strSql.Append("WorkIntroduction=@WorkIntroduction,");
			strSql.Append("WorkVerify=@WorkVerify,");
			strSql.Append("UserName=@UserName,");
			strSql.Append("Recommend=@Recommend,");
			strSql.Append("RecommendTime=@RecommendTime,");
            strSql.Append("ActivityID=@ActivityID");
            strSql.Append(" where WorkID=@WorkID");
            //strSql.Append("and WorkInfo.workID=Activity.ActivityID");
			SqlParameter[] parameters = {
					new SqlParameter("@WorkName", SqlDbType.NChar,50),
					new SqlParameter("@WorkPicture", SqlDbType.NChar,250),
					new SqlParameter("@UploadTime", SqlDbType.DateTime),
					new SqlParameter("@WorkIntroduction", SqlDbType.NVarChar,-1),
					new SqlParameter("@WorkVerify", SqlDbType.NChar,10),
					new SqlParameter("@UserName", SqlDbType.NChar,50),
					new SqlParameter("@Recommend", SqlDbType.NChar,2),
					new SqlParameter("@RecommendTime", SqlDbType.DateTime),
                    new SqlParameter("@ActivityID", SqlDbType.Int,4),
					new SqlParameter("@WorkID", SqlDbType.Int,4)
					
                                        };
			parameters[0].Value = model.WorkName;
			parameters[1].Value = model.WorkPicture;
			parameters[2].Value = model.UploadTime;
			parameters[3].Value = model.WorkIntroduction;
			parameters[4].Value = model.WorkVerify;
			parameters[5].Value = model.UserName;
			parameters[6].Value = model.Recommend;
			parameters[7].Value = model.RecommendTime;
			parameters[8].Value = model.ActivityID;
            parameters[9].Value = model.WorkID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ActivityID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from WorkInfo ");
			strSql.Append(" where ActivityID=@ActivityID");
			SqlParameter[] parameters = {
					new SqlParameter("@ActivityID", SqlDbType.Int,4)
			};
			parameters[0].Value = ActivityID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
        //public bool Delete(int WorkID)
        //{
			
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from WorkInfo ");
        //    strSql.Append(" where WorkID=@WorkID ");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@WorkID", SqlDbType.Int,4)			};
        //    parameters[0].Value = WorkID;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string ActivityIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from WorkInfo ");
			strSql.Append(" where ActivityID in ("+ActivityIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ly.Model.WorkInfo GetModel(int workid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 WorkID,WorkName,WorkPicture,UploadTime,WorkIntroduction,WorkVerify,UserName,ActivityID,Recommend,RecommendTime from WorkInfo ");
			strSql.Append(" where WorkID=@WorkID");
			SqlParameter[] parameters = {
					new SqlParameter("@WorkID", SqlDbType.Int,4)
			};
            parameters[0].Value = workid;

			ly.Model.WorkInfo model=new ly.Model.WorkInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ly.Model.WorkInfo DataRowToModel(DataRow row)
		{
			ly.Model.WorkInfo model=new ly.Model.WorkInfo();
			if (row != null)
			{
				if(row["WorkID"]!=null && row["WorkID"].ToString()!="")
				{
					model.WorkID=int.Parse(row["WorkID"].ToString());
				}
				if(row["WorkName"]!=null)
				{
					model.WorkName=row["WorkName"].ToString();
				}
				if(row["WorkPicture"]!=null)
				{
					model.WorkPicture=row["WorkPicture"].ToString();
				}
				if(row["UploadTime"]!=null && row["UploadTime"].ToString()!="")
				{
					model.UploadTime=DateTime.Parse(row["UploadTime"].ToString());
				}
				if(row["WorkIntroduction"]!=null)
				{
					model.WorkIntroduction=row["WorkIntroduction"].ToString();
				}
				if(row["WorkVerify"]!=null)
				{
					model.WorkVerify=row["WorkVerify"].ToString();
				}
				if(row["UserName"]!=null)
				{
					model.UserName=row["UserName"].ToString();
				}
				if(row["ActivityID"]!=null && row["ActivityID"].ToString()!="")
				{
					model.ActivityID=int.Parse(row["ActivityID"].ToString());
				}
				if(row["Recommend"]!=null)
				{
					model.Recommend=row["Recommend"].ToString();
				}
				if(row["RecommendTime"]!=null && row["RecommendTime"].ToString()!="")
				{
					model.RecommendTime=DateTime.Parse(row["RecommendTime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select WorkID,WorkName,WorkPicture,UploadTime,WorkIntroduction,WorkVerify,UserName,ActivityID,Recommend,RecommendTime ");
			strSql.Append(" FROM WorkInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}









        public DataSet GetList2(int workID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select WorkID,WorkName,WorkPicture,UploadTime,WorkIntroduction,UserName");
            strSql.Append(" FROM WorkInfo");
            strSql.Append(" where WorkID =@WorkID");
            SqlParameter[] parameters = {
					new SqlParameter("@WorkID", SqlDbType.Int,4)			};
            parameters[0].Value = workID;
            return DbHelperSQL.Query(strSql.ToString());
        }



        public DataSet GetList4(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 4 WorkID,WorkName,WorkPicture,UploadTime,WorkIntroduction,WorkVerify,UserName,ActivityID,Recommend,RecommendTime ");
            strSql.Append(" FROM WorkInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }









        /// <summary>
        /// 根据workid得到一个dataset
        /// </summary>
        /// <param name="workID"></param>
        /// <returns></returns>
        public DataSet GetListByworkid(int workID)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append("select WorkID,WorkName,WorkPicture,WorkIntroduction,UserName,ActivityID,disWork.disConnect,disWork.UserName ");
            strSql.Append("select WorkInfo.WorkID,WorkInfo.WorkName,WorkInfo.WorkPicture,WorkIntroduction,disWork.disContent,disWork.UserName ");
            strSql.Append(" FROM WorkInfo join disWork ");
            strSql.Append(" on WorkInfo.WorkID = disWork.WorkID");
            strSql.Append(" where WorkInfo.WorkID = @WorkID");
            SqlParameter[] parameters = {
					new SqlParameter("@WorkID", SqlDbType.Int,4)			};
            parameters[0].Value = workID;
            return DbHelperSQL.Query(strSql.ToString(),parameters);
        }



		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" WorkID,WorkName,WorkPicture,UploadTime,WorkIntroduction,WorkVerify,UserName,ActivityID,Recommend,RecommendTime ");
			strSql.Append(" FROM WorkInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM WorkInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ActivityID desc");
			}
			strSql.Append(")AS Row, T.*  from WorkInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "WorkInfo";
			parameters[1].Value = "ActivityID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

